<template>

  <div class="app-container">
    <div class="app-container" style="width:  200px;height: 500px;float: left" >
      冷库列表
      <el-tree :data="tree" :props="defaultProps" @node-click="getSlot"></el-tree>
    </div>
    <div style="float: left;">
      <div>
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="addRow"
          v-hasPermi="['system:post:add']"
        >新增行</el-button>
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="addColumn"
          v-hasPermi="['system:post:add']"
        >新增列</el-button>
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="addDepth"
          v-hasPermi="['system:post:add']"
        >新增层</el-button>
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          @click="delRow"
          v-hasPermi="['system:post:remove']"
        >减少行</el-button>
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          @click="delColumn"
          v-hasPermi="['system:post:remove']"
        >减少列</el-button>
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          @click="delDepth"
          v-hasPermi="['system:post:remove']"
        >减少层</el-button>

        <!-- 外层循环：depthNum -->
        <div v-for="i in depthNum" :key="i">


          <table style="margin: 20px; width: 100px; border-collapse: collapse; border: 1px solid #000;">
            <!-- 中层循环：rowNum -->
            <tr v-for="j in rowNum" :key="j">
              <!-- 内层循环：columnNum -->
              <td
                v-for="k in columnNum"
                :key="k"
                style="padding: 15px; font-size: 16px; border: 1px solid #ddd; text-align: center;"
              >
                {{ j }}_{{ k }}_{{ i }} <!-- 单元格内容 -->
              </td>
            </tr>
            </table>
        </div>
      </div>
    </div>

  </div>
</template>

<script>
import {getColdStorageIdAndName,getSmallColdStorageIdAndName,getAllBySubId,addSlot,delSlot, listPost, getPost, delPost, addPost, updatePost } from "@/api/storage/coldSlot";
import store from "@/store";
export default {
  name: "Post",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      userId:store.state.user.id, //将缓存的用户id赋值给userId
      userName:store.state.user.name, //将缓存的用户id赋值给userId


      tree:[],
      defaultProps: {
        children: 'children',
        label: 'label'
      },


      coldStorage: {},
      smallColdStorage:{},
      nowSubId: 0,
      //总集合
      slotList: [],
      //行集合
      rowNum:0,
      //列集合
      columnNum:0,
      //深度集合
      depthNum:0,
      goAdd:[],
      goDel: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        postCode: undefined,
        postName: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        postName: [
          { required: true, message: "岗位名称不能为空", trigger: "blur" }
        ],
        postCode: [
          { required: true, message: "岗位编码不能为空", trigger: "blur" }
        ],
        postSort: [
          { required: true, message: "岗位顺序不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getStorge();
  },
  methods: {
    //获得对应小冷库版位信息
    getSlot(some){
      if(some === this.nowSubId){
        getAllBySubId(some).then(response => {
          this.slotList = response.data;
          console.log(this.slotList);
          //赋值进行
          for (let i = 0; i < this.slotList.length; i++){
            if(this.slotList[i].row>=this.rowNum){
              this.rowNum = this.slotList[i].row;
            }
            if(this.slotList[i].column>=this.columnNum){
              this.columnNum = this.slotList[i].column;
            }
            if(this.slotList[i].depth>=this.depthNum){
              this.depthNum = this.slotList[i].depth;
            }
          }
        });
        return;
      }
      this.rowNum = 0;
      this.columnNum = 0;
      this.depthNum = 0;
      // 获取 some.label 字符串
      const label = some.label;
      for (let i = 0; i < this.tree.length; i++){
        if(label === this.tree[i].label){
          return;
        }
      }
      // 使用空格分隔字符串
      const parts = label.split(' ');
      // 获取第一个被分出来的字符串
      const firstPart = parts[0];
      this.nowSubId = firstPart;
      // 输出结果
      console.log(firstPart);
      getAllBySubId(firstPart).then(response => {
        this.slotList = response.data;
        console.log(this.slotList);
        //赋值进行
        for (let i = 0; i < this.slotList.length; i++){
          if(this.slotList[i].row>=this.rowNum){
            this.rowNum = this.slotList[i].row;
          }
          if(this.slotList[i].column>=this.columnNum){
            this.columnNum = this.slotList[i].column;
          }
          if(this.slotList[i].depth>=this.depthNum){
            this.depthNum = this.slotList[i].depth;
          }
        }
      });
    },
    /** 添加行 */
    addRow(){
      var myNum = this.rowNum + 1;
      if(this.rowNum===0||this.columnNum===0||this.depthNum===0){
        this.rowNum = 1;
        this.columnNum = 1;
        this.depthNum = 1;
        myNum = 1;
      }
      console.log(this.rowNum+"--"+this.columnNum+"--"+this.depthNum);
      for (let i = 1; i <= this.columnNum; i++){
        for (let j = 1; j <= this.depthNum; j++){
          this.goAdd.push({
            row: myNum,
            column: i,
            depth: j,
            subId: this.nowSubId,
            slotName: myNum+"_"+i+"_"+j,
            createBy: this.userName,
          })
        }
      }
      addSlot(this.goAdd).then(response => {
        this.goAdd = [];
        this.rowNum = myNum;
        this.getSlot(this.nowSubId);
      });
    },
    /** 添加列 */
    addColumn() {
      var myNum = this.columnNum + 1;
      if(this.rowNum===0||this.columnNum===0||this.depthNum===0){
        this.rowNum = 1;
        this.columnNum = 1;
        this.depthNum = 1;
        myNum = 1;
      }
      for (let i = 1; i <= this.rowNum; i++){
        for (let j = 1; j <= this.depthNum; j++){
          this.goAdd.push({
            row: i,
            column: myNum,
            depth: j,
            subId: this.nowSubId,
            slotName: i+"_"+myNum+"_"+j,
            createBy: this.userName,
          })
        }
      }
      addSlot(this.goAdd).then(response => {
        this.goAdd = [];
        this.columnNum = myNum;
        this.getSlot(this.nowSubId);
      });
    },
    /** 添加高 */
    addDepth() {
      var myNum = this.depthNum + 1;
      if(this.rowNum===0||this.columnNum===0||this.depthNum===0){
        this.rowNum = 1;
        this.columnNum = 1;
        this.depthNum = 1;
        myNum = 1;
      }
      for (let i = 1; i <= this.rowNum; i++){
        for (let j = 1; j <= this.columnNum; j++){
          this.goAdd.push({
            row: i,
            column: j,
            depth: myNum,
            subId: this.nowSubId,
            slotName: i+"_"+j+"_"+myNum,
            createBy: this.userName,
          })
        }
      }
      addSlot(this.goAdd).then(response => {
        this.goAdd = [];
        this.depthNum = myNum;
        this.getSlot(this.nowSubId);
      });
    },
    /** 删除行 */
    delRow(){
      var myNum = this.rowNum;
      for (let i = 1; i <= this.columnNum; i++){
        for (let j = 1; j <= this.depthNum; j++){
          this.goDel.push({
            row: myNum,
            column: i,
            depth: j,
            subId: this.slotList[0].subId,
            slotName: myNum+"_"+i+"_"+j,
          })
        }
      }
      delSlot(this.goDel).then(response => {
        this.goDel = [];
        this.rowNum = myNum-1;
        this.getSlot(this.nowSubId);
      });
    },
    /** 删除列 */
    delColumn(){
      var myNum = this.columnNum;
      for (let i = 1; i <= this.rowNum; i++){
        for (let j = 1; j <= this.depthNum; j++){
          this.goDel.push({
            row: i,
            column: myNum,
            depth: j,
            subId: this.slotList[0].subId,
            slotName: i+"_"+myNum+"_"+j,
          })
        }
      }
      delSlot(this.goDel).then(response => {
        this.goDel = [];
        this.columnNum = myNum-1;
        this.getSlot(this.nowSubId);
      });
    },
    /** 删除高 */
    delDepth(){
      var myNum = this.depthNum;
      for (let i = 1; i <= this.rowNum; i++){
        for (let j = 1; j <= this.columnNum; j++){
          this.goDel.push({
            row: i,
            column: j,
            depth: myNum,
            subId: this.slotList[0].subId,
            slotName: i+"_"+j+"_"+myNum,
          })
        }
      }
      delSlot(this.goDel).then(response => {
        this.goDel = [];
        this.depthNum = myNum-1;
        this.getSlot(this.nowSubId);
      });
    },
    /** 获得父冷库与小冷库信息并赋值 */
    getStorge() {
      getColdStorageIdAndName().then(response => {
        this.coldStorage = response.data;
        console.log(this.coldStorage);

        getSmallColdStorageIdAndName().then(response => {
          this.smallColdStorage = response.data;
          console.log(this.smallColdStorage);

          // 在所有数据都加载完毕后调用 getTree 方法
          this.getTree();
        });
      });
    },
    getTree() {
      this.tree = []; // 初始化 tree 数组
      // 处理 coldStorage 数据
      for (let i = 0; i < this.coldStorage.length; i++) {
        const coldStorageItem = this.coldStorage[i];
        this.tree.push({
          label: coldStorageItem.cold_name,
          children: []
        });
        for (let j = 0; j < this.smallColdStorage.length; j++){
          const smallColdStorageItem = this.smallColdStorage[j];
          if (coldStorageItem.cold_id === smallColdStorageItem.parentId) {
            this.tree[i].children.push({
              label: smallColdStorageItem.subId+" "+smallColdStorageItem.subName
            });
          }
        }
      }
    },
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      listPost(this.queryParams).then(response => {
        this.postList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        postId: undefined,
        postCode: undefined,
        postName: undefined,
        postSort: 0,
        status: "0",
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.postId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加岗位";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const postId = row.postId || this.ids
      getPost(postId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改岗位";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.postId != undefined) {
            updatePost(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPost(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const postIds = row.postId || this.ids;
      this.$modal.confirm('是否确认删除岗位编号为"' + postIds + '"的数据项？').then(function() {
        return delPost(postIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/post/export', {
        ...this.queryParams
      }, `post_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
